package spark.network.protocol;

import spark.network.Util;

public class ClockSyncReq extends Base {
	public static final short TYPE = 0x04;
	private static final byte[] TYPE_BYTES = Util.makeBytes(TYPE);

	private long requestNumber;
	private static final int REQUEST_NUMBER_OFFSET = Base.SIZE;
	private static final int REQUEST_NUMBER_LENGTH = Long.SIZE / 8;

	private static final int SIZE =
			Base.SIZE +
			REQUEST_NUMBER_LENGTH;

	public ClockSyncReq() {
		super(true, TYPE_BYTES);
	}

	public long getRequestNumber() {
		return requestNumber;
	}

	public void setRequestNumber(long requestNumber) {
		this.requestNumber = requestNumber;
	}

	@Override
	public int getSize() {
		return SIZE;
	}

	@Override
	public void fill(byte[] buffer) {
		super.fill(buffer);
		requestNumber = Util.makeLong(buffer, REQUEST_NUMBER_OFFSET);
	}

	@Override
	public void getBytes(byte[] buffer) {
		super.getBytes(buffer);
		Util.arraycopy(Util.makeBytes(requestNumber), 0, buffer, REQUEST_NUMBER_OFFSET);
	}


	public static ClockSyncReq create(byte[] buffer, long receiveTime) {
		ClockSyncReq packet = new ClockSyncReq();
		packet.fill(buffer);
		packet.setTimeStampReceive(receiveTime);
		return packet;
	}
}